import {
  Body,
  Controller,
  Get,
  Post,
  Query,
  Req,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOperation,
  ApiQuery,
} from '@nestjs/swagger';
import { Auth } from 'src/common/decorators/auth.decorator';
import { MatchEndService } from './match-end.service';
import { EndMatchDto } from './dto/endMatch.dto';
import { RequirePermission } from 'src/common/decorators/permission.decorator';
import { MatchGiveUpService } from './match-giveup.service';

@Auth()
@ApiBearerAuth()
@Controller('match')
export class MatchEndController {
  constructor(
    private readonly matchEndService: MatchEndService,
    private readonly matchGiveUpService: MatchGiveUpService,
  ) {}

  @RequirePermission(['1007001', '1007002', '1007003'], 'OR')
  @Post('endMatch')
  @UsePipes(new ValidationPipe({ transform: true }))
  @ApiOperation({
    summary: '结束比赛',
  })
  @ApiBody({
    type: EndMatchDto,
  })
  async endMatch(
    @Req() req,
    @Body()
    endMatchDto: EndMatchDto,
  ) {
    const user = req.user;
    endMatchDto.user = user;
    return this.matchEndService.endMatch(endMatchDto);
  }

  @Get('playerGiveUp')
  @ApiOperation({
    summary: '选手放弃比赛',
  })
  @ApiQuery({
    name: 'matchId',
    type: 'number',
    description: '比赛ID',
  })
  @ApiQuery({
    name: 'playerId',
    type: 'number',
    description: '选手playerId',
  })
  @ApiQuery({
    name: 'type',
    type: 'string',
    enum: ['team', 'player'],
    description: '放弃的类型',
  })
  async playerGiveUp(
    @Req() req,
    @Query('matchId') matchId: string,
    @Query('playerId') playerId: string,
    @Query('type') type: 'team' | 'player',
  ) {
    const user = req.user;
    return this.matchGiveUpService.playerGiveUp(matchId, playerId, type, {
      operator: user,
    });
  }
}
